<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace frontend\v1\controllers;

use frontend\v1\RestController;
use Qiniu\Auth;

/**
 * 功能插件接口管理
 */
class UtilController extends RestController
{
    public $actionVerbs = [
        'upload-qiniu—token' => ['GET'],
    ];

    /**
     * @api {GET} v1/util/upload-qiniu—token 云上传获取七牛云临时token（有效期3600s）
     * @apiVersion 1.0.0
     * @apiGroup 功能插件接口
     * @apiUse common_header
     * @apiUse auth_header
     * @apiSuccessExample code=200
     * {
     *     "code": 200,
     *     "data": {
     *         "token": "-fzZ6t1bajZC7fPFd0Ujw1mxyTxdmlppsVtI82pE:4RL7zXh6_Aa3Wd8dxUgWg9kmpLw=:eyJzY29wZSI6Ind4M2Mzc2hvcCIsImRlYWRsaW5lIjoxNzMwNzc2ODA4fQ==",//TOKEN过期时间3600s
     *         "domain": "http://images.demo.domain.com",//CDN域名
     *         "bucketName": "imgs"//包空间名
     *     },
     *     "msg": "Success"
     * }
     * @apiUse error500_401
     */
    public function actionUploadQiniuToken()
    {
        /** @var \builder\uploads\QiniuProvider $uploadManager */
        $uploadManager = \Yii::$app->uploadManager->provider;
        $auth = new Auth($uploadManager->accessKey, $uploadManager->secretKey);
        $accessToken = $auth->uploadToken($uploadManager->bucketName);

        return $this->asOk('Success', [
            'token' => $accessToken,
            'domain' => $uploadManager->web,
            'bucketName' => $uploadManager->bucketName,
        ]);
    }

}